package com.luckycoder.util;

import javax.sound.midi.Soundbank;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class MyInvocationHandle implements InvocationHandler {
    //创建委托对象
    private Object object = null;

    //返回代理对象(代理对象具有委托对象的全部功能)
    public Object bind(Object object) {
        this.object = object;
        Object o = Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
        return o;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(method.getName() + "方法执行了……，方法参数是：" + Arrays.toString(args));
        //由委托对象执行业务方法
        Object result = method.invoke(this.object, args);
        System.out.println("方法执行结束……");
        return result;
    }
}
